package net.easyjava.framework.web.security;

import net.easyjava.tools.BaseBean;
import net.easyjava.tools.lang.StringTools;
import net.easyjava.tools.security.aes.AESTools;
import net.easyjava.tools.security.md5.Md5Tools;

/**
 * Created by wxnacy on 15/12/22.
 */
public class UserSecurityBean extends BaseBean {
    private static final long serialVersionUID = -2330873571458842163L;
    private String userId;//用户id
    private String userIp;//用户ip
    private long timestamp;//登陆时间戳
    private long maxAge;//过期时间：毫秒
    private String sign;//二次加密

    /**
     *
     * @param userId
     * @param userIp
     * @param maxAge 设置0为会话形式
     */
    public UserSecurityBean(String userId, String userIp,long maxAge) {
        if(StringTools.isNull(userId) || StringTools.isNull(userIp)){
            throw new IllegalArgumentException("userId or userIp can't be null");
        }
        this.userId = userId;
        this.userIp = userIp;
        this.maxAge = maxAge;
        this.timestamp = System.currentTimeMillis();
    }

    public UserSecurityBean(String value) {
        String[] values = value.split(";");
        this.userId = values[0];
        this.userIp = values[1];
        this.timestamp = Long.valueOf(values[2]);
        this.maxAge = Long.valueOf(values[3]);
        this.sign = values[4];
        if(StringTools.isNull(userId) || StringTools.isNull(userIp)|| StringTools.isNull(sign) || this.timestamp<=0){
            throw new IllegalArgumentException("value is error");
        }


    }

    public String toSecurityString(){
        return String.format("%s;%s;%s;%s",userId,userIp,timestamp,maxAge);
    }

    public String toSecuritySignString(String sign){
        this.sign = sign;
        return String.format("%s;%s;%s;%s;%s",userId,userIp,timestamp,maxAge,this.sign);
    }

    public String getSign() {
        return sign;
    }

    public String getUserId() {
        return userId;
    }

    public String getUserIp() {
        return userIp;
    }

    public long getTimestamp() {
        return timestamp;
    }

    public long getMaxAge() {
        return maxAge;
    }


    public static void main(String[] args) {
        String sign = getSecurity();
        UserSecurityBean userSecurityBean = getUserSecurityBean(sign);
    }

    public static UserSecurityBean getUserSecurityBean(String userSecurity){
        AESTools aesTools = new AESTools("5c948342a88fb378467a1eb8a7afd485");
        String SIGN_KEY = "3b7bcc0448703bd5abde58a2aa3fa7c6";
        UserSecurityBean userSecurityBean = new UserSecurityBean("userId","userIp",1000);
        //将信息加密
        try {



            userSecurity = aesTools.decrypt(userSecurity);
            userSecurityBean = new UserSecurityBean(userSecurity);

            userSecurity = aesTools.encrypt(userSecurityBean.toSecurityString());
            if (userSecurityBean.getSign().equalsIgnoreCase(Md5Tools.md5(userSecurity+SIGN_KEY))){
                System.out.println("userSecurityBean = " + userSecurityBean.toString());
                return userSecurityBean;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }


    public static String getSecurity(){
        AESTools aesTools = new AESTools("5c948342a88fb378467a1eb8a7afd485");
        String SIGN_KEY = "3b7bcc0448703bd5abde58a2aa3fa7c6";
        UserSecurityBean userSecurityBean = new UserSecurityBean("userId","userIp",1000);
        //将信息加密
        String userSecurity = null;
        try {
            userSecurity = aesTools.encrypt(userSecurityBean.toSecurityString());
            //生成sign
            String userSecuritySign = Md5Tools.md5(userSecurity + SIGN_KEY);
            //二次加密
            userSecurity = aesTools.encrypt(userSecurityBean.toSecuritySignString(userSecuritySign));
            System.out.println("userSecuritySign = " + userSecuritySign);

            return userSecurity;

        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
}
